package com.springboot_gradrecruit.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.springboot_gradrecruit.common.PageResponse;
import com.springboot_gradrecruit.common.R;
import com.springboot_gradrecruit.exception.CustomException;
import com.springboot_gradrecruit.model.dto.UserLoginRequest;
import com.springboot_gradrecruit.model.dto.UserLogoutRequest;
import com.springboot_gradrecruit.model.dto.UserRegisterRequest;
import com.springboot_gradrecruit.model.dto.admin.PageUserRequest;
import com.springboot_gradrecruit.model.dto.admin.UserRequest;
import com.springboot_gradrecruit.model.entity.*;
import com.springboot_gradrecruit.model.vo.UserVO;
import com.springboot_gradrecruit.model.vo.student.StudentVO;
import com.springboot_gradrecruit.service.*;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author zhaorong
 */
@Tag(name = "用户相关接口")
@RestController
@RequestMapping("/user")
@Slf4j
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private StudentService studentService;

    @Resource
    private EnterpriseService enterpriseService;

    @Resource
    private VideoService videoService;

    @Resource
    private ReportService reportService;


    // 注册
    @Operation(summary = "注册")
    @PostMapping("/register")
    public R<UserVO> register(@RequestBody UserRegisterRequest userRegisterRequest) {
        //1. 参数校验
        if (userRegisterRequest == null) {
            return R.error(null, "参数为空");
        }
        //2。. 调用service完成注册
        UserVO register = userService.register(userRegisterRequest);
        return R.success(register, "注册成功");
    }

    // 登录
    @Operation(summary = "登录")
    @PostMapping("/login")
    public R<UserVO> login(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        //1. 参数校验
        if (userLoginRequest == null || request == null) {
            return R.error(null, "参数为空");
        }
        //2. 调用service完成登录
        UserVO login = userService.login(userLoginRequest, request);
        return R.success(login, "登录成功");
    }

    // 退出
    @Operation(summary = "退出")
    @PostMapping("/logout")
    public R<String> logout(@RequestBody UserLogoutRequest userLogoutRequest, HttpServletRequest request) {
        userService.userLogout(request, userLogoutRequest);
        return R.success(null, "退出成功");

    }

    //重置密码
    @Operation(summary = "重置密码")
    @PostMapping("/resetPassword")
    public R<String> resetPassword(Long userId, String password) {
        //1. 参数校验
        if (userId == null || password == null) {
            return R.error(null, "参数为空");
        }
        //2. 调用service完成查询
        userService.resetPassword(userId, password);
        return R.success(null, "重置密码成功");
    }

    //查询
    @Operation(summary = "查询用户信息")
    @PostMapping("/queryUserById")
    public R<UserVO> queryUserById(Long userId) {
        //1.参数校验
        if (userId == null) {
            return R.error(null, "参数为空");
        }
        //2.调用service完成查询
        User userById = userService.getById(userId);
        UserVO userVO = BeanUtil.copyProperties(userById, UserVO.class);
        return R.success(userVO, "查询成功");
    }

    @Operation(summary = "查询学生信息根据用户ID")
    @PostMapping("/queryStudentByUserId")
    public R<StudentVO> queryStudentByUserId(Long userId) {
        //1.参数校验
        if (userId == null) {
            return R.error(null, "参数为空");
        }
        //2.调用service完成查询
        QueryWrapper<Student> studentQueryWrapper = new QueryWrapper<>();
        studentQueryWrapper.eq("student_user_id", userId);
        Student student = studentService.getOne(studentQueryWrapper);
        StudentVO studentVO = BeanUtil.copyProperties(student, StudentVO.class);
        return R.success(studentVO, "查询成功");
    }

    //分页查询用户
    @Operation(summary = "分页查询用户")
    @PostMapping("/pageUser")
    public R<PageResponse<UserVO>> pageUser(@RequestBody PageUserRequest pageUserRequest, HttpServletRequest request) {
        //1. 参数校验
        if (pageUserRequest == null || request == null) {
            return R.error(null, "参数为空");
        }
        //2. 调用service完成查询
        PageResponse<UserVO> pageUser = userService.pageUser(pageUserRequest);
        return R.success(pageUser, "查询成功");
    }

    @Operation(summary = "修改用户信息")
    @PostMapping("/updateUserById")
    public R<Boolean> updateUserById(@RequestBody UserRequest userRequest, HttpServletRequest request) {
        //1. 参数校验
        if (userRequest == null || request == null) {
            return R.error(null, "参数为空");
        }
        //2. 调用service完成查询
        boolean updateUser = userService.updateUserById(userRequest);
        if (updateUser) {
            return R.success(null, "修改成功");
        }
        return R.error(null, "修改失败");
    }

    @Operation(summary = "管理员管理学生")
    @PostMapping("/updateUserAndStudentById")
    public R<Boolean> updateUserAndStudentById(Long studentId, Long studentUserId, Integer userStatus) {
        //1. 参数校验
        if (studentId == null || studentUserId == null || userStatus == null) {
            return R.error(null, "参数为空");
        }
        //2. 调用service完成查询
        try {
            // 先从学生服务中根据学生ID获取学生相关信息
            Student student = studentService.getById(studentId);
            if (student == null) {
                return R.error(null, "找不到对应的学生记录");
            }
            // 再从用户服务中根据用户ID获取用户相关信息
            User user = userService.getById(studentUserId);
            if (user == null) {
                return R.error(null, "找不到对应的用户记录");
            }

            // 3. 修改用户状态
            user.setUserStatus(userStatus);
            boolean updateUserResult = userService.updateById(user);
            if (!updateUserResult) {
                return R.error(null, "更新用户状态失败");
            }

            // 4. 修改学生状态
            student.setStudentStatus(userStatus);
            boolean updateStudentResult = studentService.updateById(student);
            if (!updateStudentResult) {
                return R.error(null, "更新学生关联的用户状态失败");
            }

            return R.success(null, "更新成功");
        } catch (Exception e) {
            log.error("更新用户和学生状态出现异常", e);
            return R.error(null, "更新出现异常");
        }
    }

    @Operation(summary = "管理员管理企业")
    @PostMapping("/updateUserAndEnterpriseById")
    public R<Boolean> updateUserAndEnterpriseById(Long enterpriseId, Long enterpriseUserId, Integer userStatus) {
        //1. 参数校验
        if (enterpriseId == null || enterpriseUserId == null || userStatus == null) {
            return R.error(null, "参数为空");
        }
        //2. 调用service完成查询
        try {
            // 先从企业服务中根据企业ID获取企业相关信息
            Enterprise enterprise = enterpriseService.getById(enterpriseId);
            if (enterprise == null) {
                return R.error(null, "找不到对应的学生记录");
            }
            // 再从用户服务中根据用户ID获取用户相关信息
            User user = userService.getById(enterpriseUserId);
            if (user == null) {
                return R.error(null, "找不到对应的用户记录");
            }

            // 3. 修改用户状态
            user.setUserStatus(userStatus);
            boolean updateUserResult = userService.updateById(user);
            if (!updateUserResult) {
                return R.error(null, "更新用户状态失败");
            }

            // 4. 修改企业状态
            enterprise.setEnterpriseStatus(userStatus);
            boolean updateEnterpriseResult = enterpriseService.updateById(enterprise);
            if (!updateEnterpriseResult) {
                return R.error(null, "更新企业关联的用户状态失败");
            }

            return R.success(null, "更新成功");
        } catch (Exception e) {
            log.error("更新用户和企业状态出现异常", e);
            return R.error(null, "更新出现异常");
        }
    }

    // 删除用户
    @Operation(summary = "删除用户")
    @PostMapping("/deleteUserById")
    public R<Boolean> deleteUserById(@RequestBody UserRequest userRequest, HttpServletRequest request) {
        //1. 参数校验
        if (userRequest == null || request == null) {
            return R.error(null, "参数为空");
        }
        //2. 调用service完成查询
        boolean deleteUser = userService.removeById(userRequest.getUserId());

        if (deleteUser) {
            return R.success(null, "删除成功");
        }
        return R.error(null, "删除失败");

    }

    //查询审核视频状态在审核中的数量
    @Operation(summary = "查询视频状态在审核中的数量")
    @PostMapping("/queryVideoStatusCount")
    public R<Long> queryVideoStatusCount(String videoCertificationStatus) {

        //1. 参数校验
        if (videoCertificationStatus == null) {
            throw new RuntimeException("参数为空");
        }

        //2. 调用service完成查询
        QueryWrapper<Video> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("video_certification_status", videoCertificationStatus);
        return R.success(videoService.count(queryWrapper), "查询成功");

    }

    //查询企业审核状态在审核中的数量
    @Operation(summary = "查询企业状态在审核中的数量")
    @PostMapping("/queryEnterpriseStatusCount")
    public R<Long> queryEnterpriseStatusCount(String enterpriseCertificationStatus) {

        //1. 参数校验
        if (enterpriseCertificationStatus == null) {
            throw new RuntimeException("参数为空");
        }

        //2. 调用service完成查询
        QueryWrapper<Enterprise> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("enterprise_certification_status", enterpriseCertificationStatus);
        return R.success(enterpriseService.count(queryWrapper), "查询成功");

    }

    //查询反馈举报状态在审核中的数量
    @Operation(summary = "查询反馈举报状态在审核中的数量")
    @PostMapping("/queryReportStatusCount")
    public R<Long> queryReportStatusCount(String reportCheckStatus) {

        //1. 参数校验
        if (reportCheckStatus == null) {
            throw new RuntimeException("参数为空");
        }

        //2. 调用service完成查询
        QueryWrapper<Report> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("report_check_status", reportCheckStatus);
        return R.success(reportService.count(queryWrapper), "查询成功");

    }

    //用户上传头像
    @Operation(summary = "用户上传头像")
    @PostMapping("/userUploadAvatar")
    public R<String> userUploadAvatar(String userId,String imageUrl) {
        return userService.userUploadAvatar(Long.valueOf(userId), imageUrl);
    }


}

